package com.ruoyi.asset.service.impl;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.asset.common.AssetStatusType;
import com.ruoyi.asset.common.MaintenanceType;
import com.ruoyi.asset.domain.FixedAssets;
import com.ruoyi.asset.domain.vo.MaintenanceStatementVO;
import com.ruoyi.asset.mapper.FixedAssetsMapper;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.MaintenanceRecordMapper;
import com.ruoyi.asset.domain.MaintenanceRecord;
import com.ruoyi.asset.service.IMaintenanceRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 维修保养记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-06
 */
@Service
public class MaintenanceRecordServiceImpl extends ServiceImpl<MaintenanceRecordMapper, MaintenanceRecord> implements IMaintenanceRecordService {
    @Autowired
    private MaintenanceRecordMapper maintenanceRecordMapper;


    @Autowired
    private FixedAssetsMapper fixedAssetsMapper;


    /**
     * 查询维修保养记录
     *
     * @param id 维修保养记录主键
     * @return 维修保养记录
     */
    @Override
    public MaintenanceRecord selectMaintenanceRecordById(String id) {
        return maintenanceRecordMapper.selectMaintenanceRecordById(id);
    }

    /**
     * 查询维修保养记录列表
     *
     * @param maintenanceRecord 维修保养记录
     * @return 维修保养记录
     */
    @Override
    public List<MaintenanceRecord> selectMaintenanceRecordList(MaintenanceRecord maintenanceRecord) {

        LambdaQueryWrapper<MaintenanceRecord> mrlqw = new LambdaQueryWrapper<>();
        mrlqw.eq(MaintenanceRecord::getMaintenanceContent, maintenanceRecord.getMaintenanceDate());

        return maintenanceRecordMapper.selectMaintenanceRecordList(maintenanceRecord);
    }

    /**
     * 新增维修保养记录
     *
     * @param maintenanceRecord 维修保养记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMaintenanceRecord(MaintenanceRecord maintenanceRecord) {
        //判断前端有没有穿id过来 如果没有旧使用uuid自动生成一个id
        String id = maintenanceRecord.getId();
        if (StringUtils.isEmpty(id)) {
            maintenanceRecord.setId(UUID.randomUUID().toString());
        }
        //查询资产是否存在
        String assetNumber = maintenanceRecord.getAssetNumber();
        FixedAssets fixedAssets = null;

        fixedAssets = fixedAssetsMapper.selectById(assetNumber);

        if (fixedAssets == null) {
            throw new UtilException("该资产编号不存在");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.MAINTENANCE)) {
            throw new UtilException(assetNumber + "正在维修不能新建维修记录");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.TO_BE_SCRAPPED)) {
            throw new UtilException(assetNumber + "是待报废状态不能下新建维修记录");
        }
        if (Objects.equals(fixedAssets.getAssetStatus(), AssetStatusType.SCRAPPED)) {
            throw new UtilException(assetNumber + "是报废状态不能下新建维修记录");
        }
        //获取维修状态如果为维修或者保养就更新资产状态为维修状态
        String maintenanceType = maintenanceRecord.getMaintenanceType();
        if (Objects.equals(maintenanceType, MaintenanceType.MAINTENANCE) || Objects.equals(maintenanceType, MaintenanceType.MAINTAIN)) {
            fixedAssets.setAssetStatus(AssetStatusType.MAINTENANCE);
            fixedAssetsMapper.updateById(fixedAssets);
        }


        return maintenanceRecordMapper.insert(maintenanceRecord);
    }

    /**
     * 修改维修保养记录
     *
     * @param maintenanceRecord 维修保养记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMaintenanceRecord(MaintenanceRecord maintenanceRecord) {

        String assetNumber = maintenanceRecord.getAssetNumber();
        //查询当前数据是否是已完成的
        MaintenanceRecord oLdMaintenanceRecord = maintenanceRecordMapper.selectById(maintenanceRecord.getId());
        if (oLdMaintenanceRecord.getMaintenanceType().equals(MaintenanceType.NORMAL)) {
            throw new RuntimeException("已完成的维修记录不能修改");
        }

        FixedAssets fixedAssets = fixedAssetsMapper.selectById(assetNumber);
        String maintenanceType = maintenanceRecord.getMaintenanceType();
        if (maintenanceType.equals(MaintenanceType.NORMAL)) {
            fixedAssets.setAssetStatus(AssetStatusType.NORMAL);
            fixedAssetsMapper.updateById(fixedAssets);
        }

        return maintenanceRecordMapper.updateById(maintenanceRecord);
    }

    /**
     * 批量删除维修保养记录
     *
     * @param ids 需要删除的维修保养记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaintenanceRecordByIds(String ids) {
        if (ids != null) {
            String[] strings = Convert.toStrArray(ids);
            for (String id : strings) {
                MaintenanceRecord maintenanceRecord = maintenanceRecordMapper.selectById(id);
                if (Objects.equals(maintenanceRecord.getMaintenanceType(), MaintenanceType.NORMAL)) {
                    throw new UtilException("请勿选择正常状态的资产！");
                }
                FixedAssets fixedAssets = new FixedAssets();
                fixedAssets.setAssetNumber(maintenanceRecord.getAssetNumber());
                fixedAssets.setAssetStatus(AssetStatusType.NORMAL);
                fixedAssetsMapper.updateById(fixedAssets);
                maintenanceRecord.setMaintenanceType(MaintenanceType.NORMAL);
                maintenanceRecordMapper.updateById(maintenanceRecord);
            }
            return 1;
        }
        throw new UtilException("取消错误");
        //        return maintenanceRecordMapper.deleteMaintenanceRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 取消维修保养记录信息
     *
     * @param id 取消保养记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaintenanceRecordById(String id) {
        MaintenanceRecord oLdMaintenanceRecord = maintenanceRecordMapper.selectById(id);
        if (oLdMaintenanceRecord.getMaintenanceType().equals(MaintenanceType.NORMAL)) {
            throw new RuntimeException("已完成的维修记录不能取消！");
        }
        FixedAssets fixedAssets = new FixedAssets();
        fixedAssets.setAssetNumber(oLdMaintenanceRecord.getAssetNumber());
        fixedAssets.setAssetStatus(AssetStatusType.NORMAL);
        fixedAssetsMapper.updateById(fixedAssets);
        oLdMaintenanceRecord.setMaintenanceType(MaintenanceType.NORMAL);
        return maintenanceRecordMapper.updateById(oLdMaintenanceRecord);
//        return maintenanceRecordMapper.deleteMaintenanceRecordById(id);
    }

    @Override
    public List<MaintenanceStatementVO> selectMaintenanceStatement(MaintenanceRecord maintenanceRecord) {
        Map<String, Object> params = maintenanceRecord.getParams();
        LocalDate beginRecordDate = LocalDate.parse((String)params.get("beginRecordDate"));
        LocalDate endRecordDate = LocalDate.parse((String)params.get("endRecordDate"));

        List<MaintenanceStatementVO> maintenanceStatementVOS =
                maintenanceRecordMapper.countByMaintenanceContentBetweenDate(beginRecordDate, endRecordDate);


        return maintenanceStatementVOS;
    }


}
